//
//  FirstViewController.m
//  We_Travel
//
//  Created by lanou3g on 15/12/10.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "FirstViewController.h"
#import "RESideMenu.h"
#import "FirstTableViewCell.h"
#import "NH_ViewModel.h"
#import "UIImageView+WebCache.h"
#import "NH_IDHelpers.h"
#import "DestinationTableViewController.h"
#import "CoreDataToolHelper.h"
#import "ArrangementHelper.h"
#import "MyTravelArrangeCell.h"
#import "CompleteItineratyController.h"
#import "SiteManager.h"
#import "Site.h"


@interface FirstViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *seletedTravelArray;
@property (nonatomic,assign) UITableViewCellEditingStyle *style;
@end

@implementation FirstViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupNavigation];
    [self.tableVIew reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    [self.tableVIew registerNib:[UINib nibWithNibName:@"FirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"firstCell_id"];
    [self.tableVIew registerNib:[UINib nibWithNibName:@"MyTravelArrangeCell" bundle:nil] forCellReuseIdentifier:@"myTravelArrange"];
    [self setupNavigation];
    self.tableVIew.delegate = self;
    self.tableVIew.dataSource = self;
    self.tableVIew.bounces = NO;
    [self loadData];
    NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]);

}
- (void)setupNavigation{
    if ([[CoreDataToolHelper shareCoreDataTool]fetchCountry].count >0 && !self.isPush){
        self.title = @"我的行程";
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftButtonImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"RightBarButton"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    }else{
        self.title = @"新建行程";
        if (self.isPush) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backButonAction:)];
        }else{
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftButtonImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
            self.navigationItem.rightBarButtonItem = nil;
        }
    }
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
- (void)rightButtonAction:(UIBarButtonItem *)sender{
    FirstViewController  *first = [[FirstViewController alloc]init];
    first.isPush = YES;
    [self.navigationController showViewController:first sender:nil];
}
-(void)backButonAction:(UIBarButtonItem *)sender{
    FirstViewController  *first = [[FirstViewController alloc]init];
    first.isPush = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  网络请求数据
 */
- (void)loadData {
  [[ArrangementHelper shareArrangementHelper]setResultBlock:^{
      [self.tableVIew reloadData];
  }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([[CoreDataToolHelper shareCoreDataTool]fetchCountry].count > 0 && !self.isPush) {
    return [[CoreDataToolHelper shareCoreDataTool]fetchCountry].count;
    }else{
    return [ArrangementHelper shareArrangementHelper].travelArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[CoreDataToolHelper shareCoreDataTool]fetchCountry].count > 0 && !self.isPush) {
        MyTravelArrangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myTravelArrange" forIndexPath:indexPath];
        self.seletedTravelArray =  [[ArrangementHelper shareArrangementHelper]selectdTravelArray];
        if (self.seletedTravelArray.count > 0) {
            NH_ViewModel *model = self.seletedTravelArray[indexPath.row];
            [cell.imageCountry sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.cover]]];
            cell.CountryNameLabel.text = [NSString stringWithFormat:@"%@行程",model.name];
        }
        return cell;
    }else{
              FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstCell_id" forIndexPath:indexPath];
              if (!cell) {
                  cell = [[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCell_id"];
              }
              NH_ViewModel *model = [ArrangementHelper shareArrangementHelper].travelArray[indexPath.row];
              cell.chinaCountriesLabel.text = model.name;
              cell.enCountriesLabel.text = model.name_en;
              cell.weatherLabel.text = [NSString stringWithFormat:@"%.1f℃",model.temperature];
              cell.timeLabel.text = model.time;
              cell.kmLabel.text = model.distance;
              [cell.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.cover]]];
              return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
//实现删除行程功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([[CoreDataToolHelper shareCoreDataTool]fetchCountry].count > 0 && !self.isPush){
        return YES;
    }
    else{
        return NO;
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [[CoreDataToolHelper shareCoreDataTool]deleteTravelCountry:indexPath.row];
    [self setupNavigation];
    [self.tableVIew reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
   // NSLog(@"%s,%d",__FUNCTION__,__LINE__);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [SiteManager shareSiteManager].countryCount = indexPath.row;
    if ([[CoreDataToolHelper shareCoreDataTool]fetchCountry].count > 0 && !self.isPush){
        [SiteManager shareSiteManager].isNewPushCom = NO;
        CompleteItineratyController *civc = [[CompleteItineratyController alloc]init];
        [self.navigationController showViewController:civc sender:nil];
    }else{
        NH_ViewModel *model = [ArrangementHelper shareArrangementHelper].travelArray[indexPath.row];
        DestinationTableViewController *dtvc = [[DestinationTableViewController alloc]init];
        dtvc.id_last = model.id_View;
        dtvc.countryName = model.name;
        [self.navigationController showViewController:dtvc sender:nil];
    }
}
@end
